local Lua__lianjin = fk.CreateSkill({
    name = "Lua__lianjin",
    tags = {Skill.Compulsory}
  })
  
  Fk:loadTranslationTable{
    ["Lua__lianjin"] = "炼金",
    [":Lua__lianjin"] = "锁定技，每回合每种花色限一次，当你使用或打出牌时，若此牌为手牌中花色最少或为1，你摸x张牌（x为你此时手牌花色最多的牌的数量）",
    ["@Lua__lianjin-turn"] = "炼金",
  }


  

Lua__lianjin:addEffect(fk.CardUsing, {
    can_trigger = function(self, event, target, player, data)
        if target == player and player:hasSkill(Lua__lianjin.name) and not table.contains(player:getTableMark("@Lua__lianjin-turn"), data.card:getSuitString(true)) then 
        
            -- 获取当前使用的牌的花色
            local cardSuit = data.card.suit
            
            -- 统计手牌中各花色的数量
            local suits = {}
            for _, id in ipairs(player:getCardIds("h")) do
                local suit = Fk:getCardById(id).suit
                    suits[suit] = (suits[suit] or 0) + 1
               
            end
            
            -- 判断当前花色是否为1
            local currentSuitCount = suits[cardSuit] or 0
            if currentSuitCount == 1 then return true end
            
            -- 判断当前花色是否是手牌中数量最少的花色
            -- 如果手牌中没有此花色的牌，也算作最少
            if currentSuitCount == 0 then return true end
            
            local minCount = math.huge
            for _, count in pairs(suits) do
                if count > 0 and count < minCount then
                    minCount = count
                end
            end
            
            return currentSuitCount > 0 and currentSuitCount == minCount
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local suit = data.card:getSuitString(true)
        player.room:addTableMark(player, "@Lua__lianjin-turn", suit)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room



        local suits = {}
        for _, id in ipairs(player:getCardIds("h")) do
            local suit = Fk:getCardById(id).suit
            suits[suit] = (suits[suit] or 0) + 1
        end
        local max = 0
        for _, num in pairs(suits) do
            if num > max then
                max = num
            end
        end
        player:drawCards(max)
    end,
})
  

Lua__lianjin:addEffect(fk.CardResponding, {
    can_trigger = function(self, event, target, player, data)
        if target == player and player:hasSkill(Lua__lianjin.name) and not table.contains(player:getTableMark("@Lua__lianjin-turn"), data.card:getSuitString(true)) then 
        
            -- 获取当前使用的牌的花色
            local cardSuit = data.card.suit
            
            -- 统计手牌中各花色的数量
            local suits = {}
            for _, id in ipairs(player:getCardIds("h")) do
                local suit = Fk:getCardById(id).suit
                    suits[suit] = (suits[suit] or 0) + 1
               
            end
            
            -- 判断当前花色是否为1
            local currentSuitCount = suits[cardSuit] or 0
            if currentSuitCount == 1 then return true end
            
            -- 判断当前花色是否是手牌中数量最少的花色
            -- 如果手牌中没有此花色的牌，也算作最少
            if currentSuitCount == 0 then return true end
            
            local minCount = math.huge
            for _, count in pairs(suits) do
                if count > 0 and count < minCount then
                    minCount = count
                end
            end
            
            return currentSuitCount > 0 and currentSuitCount == minCount
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local suit = data.card:getSuitString(true)
        player.room:addTableMark(player, "@Lua__lianjin-turn", suit)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room



        local suits = {}
        for _, id in ipairs(player:getCardIds("h")) do
            local suit = Fk:getCardById(id).suit
            suits[suit] = (suits[suit] or 0) + 1
        end
        local max = 0
        for _, num in pairs(suits) do
            if num > max then
                max = num
            end
        end
        player:drawCards(max)
    end,
})
 
  
  return Lua__lianjin
  